﻿Ext.regModel('Key',{
	fields:[
	    {name:'kind',type:'string'},
	    {name:'id',type:'string'}
	]
});

Ext.regModel('ComplexKey',{
	fields:[
		    {name:'kind',type:'string'},
		    {name:'id',type:'string'},
		    {name:'parentKey',model:'Key'}
		]
});


//信息分类
Ext.regModel('User',{
	fields:[
	    {name:"email",type:"string"},
        {name:"userId",type:"string"},
        {name:"authDomain",type:"string"}
    ]
});
Ext.regModel('InfoKind',{
	fields:[
		{name:'id',type:'string'},
		{name:'name',type:'string'},
		{name:'desc',type:'string'},
		{name:'addedBy',model:'User'},
		{name:'addedTime',type:'string'}
	]
});

//信息
Ext.regModel('Information',{
	fields:[
		{name:'id',type:'string'},
		{name:'inforKind',model:'InfoKind'},
		{name:'name',type:'string'},
		{name:'txt',type:'string'},
		{name:'desc',type:'string'},
		{name:'addedBy',type:'User'},
		{name:'addedTime',type:'string'}
	]
});
//用户分组

Ext.regModel('AccountGroup',{
	fields:[
		{name:'id',type:'string'},
		{name:'name',type:'string'},
		{name:'desc',type:'string'},
		{name:'addedBy',model:'User'},
		{name:'addedTime',type:'string'}
	]
});
//成员管理
//

Ext.regModel('AccountManager',{
	fields:[
		{name:'id',type:'string'},
		{name:'accountGroup',model:'Key'},
		{name:'name',type:'string'},
		{name:'gender',type:'string'},
		{name:'workEmail',type:'string'},
		{name:'tel',type:'string'},
		{name:'mobile',type:'string'},
		{name:'desc',type:'string'},
		{name:'addedBy',type:'User'},
		{name:'addedTime',type:'string'}
	]
});



Ext.ns('com.hp.demo.vteam.base');
Ext.ns('com.hp.demo.vteam.components');

//历史加载的组件
com.hp.demo.vteam.base.his = undefined;

//-------------------------通用方法以及类-------------------------------begin
com.hp.demo.vteam.base.status_msg = function(msg)
{
	this.doLoad(msg);
};

com.hp.demo.vteam.base.ComponentLoader = function(componentId,msg,params)
{
	var containner = this;
	var loadSub = function(sub)
	{
		if(com.hp.demo.vteam.base.his)
		{
			containner.remove(com.hp.demo.vteam.base.his,true);
		}
		com.hp.demo.vteam.base.his = new sub({params:params});
		containner.add(com.hp.demo.vteam.base.his);
		containner.doLayout();
		com.hp.demo.vteam.base.status_msg(msg + '加载完成');
	};
	if(!com.hp.demo.vteam.components[componentId])
	{
		Ext.Ajax.request({
			url:'js/com.hp.demo.vteam.components.' + componentId + '.js',
			params:params,
			success:function(r)
			{
				var obj = eval(r.responseText);
				//com.hp.demo.vteam.base.Loaded[componentId] = obj;
				loadSub(obj);
			},
			failure:function()
			{
				com.hp.demo.vteam.base.status_msg(msg + '加载失败!');
				return;
			}
		});
	}
	else
	{
		loadSub(com.hp.demo.vteam.components[componentId]);
	}
}
/**
 * 在主显示区域加载指定id的component
 * @param componentId 组件Id,将加载js目录下com.hp.demo.vteam.components.componentId.js的文件
 * @param params 传入的参数，在自定义的compoent中可以用this.params.paramKey的方式得到
 * @param msg 加载时状态栏显示的内容
 */
com.hp.demo.vteam.base.Action = function(componentId,msg,params)
{
	com.hp.demo.vteam.History.add(componentId,msg,params);
};

//创建信息的视图
com.hp.demo.vteam.base.QueryPageStore = function(conf)
{
	var callback = this;
	return new Ext.data.Store({
		model:conf.model,
		pageSize:com.hp.demo.vteam.dict.PAGE_SIZE,
		proxy:{
			type:'ajax',
			url:conf.url,
			extraParams:{method:'search',asPage:true,limit:10},
			reader:{
				type:'json',
				totalProperty:'total',
				root:'datas'
			},
			listeners:{
				exception:function()
				{
					com.hp.demo.vteam.base.status_msg(conf.errorMsg);
				}
			}
		}
	});
};
com.hp.demo.vteam.base.QuerySelectStore = function(conf)
{
	var callback = this;
	return new Ext.data.Store({
		model:conf.model,
		proxy:{
			type:'ajax',
			url:conf.url,			
			extraParams:{method:'search',asPage:false},
			reader:{
				type:'json',
				totalProperty:'total',
				root:'datas'
			},
			listeners:{
				exception:function()
				{
					com.hp.demo.vteam.base.status_msg(conf.errorMsg);
				}
			}
		}
	});
};

/**
 * 通用的有图片的跳转组件
 */
com.hp.demo.vteam.base.LogoNavPanel = Ext.extend(Ext.Container,{
	/**
	 * conf{
	 *	 componentId:点击后加载的组件,
	 *	 params:跳转的参数,
	 *   logoTitle:图标文本,
	 *   logoImage:图标,
	 *   items:
	 * }
	 */
	constructor:function(conf)
	{
		Ext.apply(this,conf);
		com.hp.demo.vteam.base.LogoNavPanel.superclass.constructor.call(this);
	},
	onclick:function(e)
	{
		com.hp.demo.vteam.base.Action(
			this.componentId,
			com.hp.demo.vteam.dict.LOGO_TITLE[this.componentId]
		);
	},
	onRender : function(ct, position){
		Ext.Panel.superclass.onRender.call(this, ct, position);
		var el = this.el,
            d = el.dom;
		var dh = Ext.DomHelper;
		logoAll = dh.append(this.id,{tag:'div',cls:"LogoNavPanel"});
		logoPanel = dh.append(logoAll,{tag:'div',cls:"vteam_tag"});
		
		this.logo = dh.append(logoPanel,"<img width=\"100\" height=\"100\" src=\"./images/" + this.logoImage + "\"/>");
		sub_contain = this.sub_contain = dh.append(logoPanel,{tag:'div',cls:"vteam_tag_item",html:this.logoTitle})
		Ext.each(this.subitems,function(item){
			var subitem = dh.append(sub_contain,{tag:'div',cls:'vteam_tag_subitem',html:item.title});
			subitem.onclick = item.command;
		},this);
	},
	initEvents:function()
	{
		this.logo.onclick = Ext.createDelegate(this.onclick,this);
	}
});
Ext.reg('logo',com.hp.demo.vteam.base.LogoNavPanel);


com.hp.demo.vteam.base.GridPanel = Ext.extend(Ext.Panel,{
	scroll: "vertical",

	create:Ext.emptyFn,

	modify:Ext.emptyFn,
	
	remove:Ext.emptyFn,

	onRowClick:Ext.emptyFn,
	
	onRowDoubleClick:Ext.emptyFn,
	
	to_prev:function()//跳转到前一页
	{
		if(this.store.currentPage != 1)
		{
			this.store.previousPage();
		}
		else
		{
			com.hp.demo.vteam.base.status_msg(com.hp.demo.vteam.dict.PAGE.ALREADY_BEGIN);
		}
	},
	
	to_next:function()//跳转到后一页
	{
		if(this.store.currentPage < this.pages_count)
		{
			this.store.nextPage();
		}
		else
		{
			com.hp.demo.vteam.base.status_msg(com.hp.demo.vteam.dict.PAGE.ALREADY_LAST);
		}
	},

	to_certain:function()
	{
		var pageIdx = this.page_ipt.value;
		if(pageIdx < 1 || pageIdx > this.pages_count)
		{
			com.hp.demo.vteam.base.status_msg(com.hp.demo.vteam.dict.PAGE.OUT_RANGE);
		}
		else
		{
			this.store.loadPage(parseInt(pageIdx));
		}
	},

	initComponent:function()
	{
		var self = this;
		//创建显示数据的对象
		self.items = self.dataview = self.buildDataView();
		
		if(!Ext.isArray(self.dockedItems))
		{
			self.dockedItems = [];
		}
        //表头创建
		self.header = new Ext.Component(self.buildHeader());
		self.dockedItems.push(self.header);
		
		if(self.pagenation)
		{
			//创建分页组件 
			self.pagenation = new Ext.Component(self.buildPagenation());
			self.dockedItems.push(self.pagenation);
		}
        //调用父类的initComponent方法
		com.hp.demo.vteam.base.GridPanel.superclass.initComponent.call(self);
		//当前数据加载后修改当前页的值
		self.store.on('load',function(){
			self.page_ipt.value = this.currentPage;
		});
		//加载数据访问服务器
		self.loadData();
	},

	buildHeader:function()
	{
		var self = this,
			colModel = self.colModel,
			colNum = self.getColNum()
			cellWidth = 100/colNum,
			colTpl    = '<table class="x-grid-header">';

		colTpl += '    <tr>';

		for (var i = 0; i < colModel.length; i++) {
			var col  = colModel[i],
				flex = col.flex || 1,
				cls  = "";
			var width = flex * cellWidth;

			/*
			if (col.hidden) {
				cls += "x-grid-col-hidden";
			}
			*/
			colTpl += '<td width="' + width + '%" class="x-grid-cell x-grid-hd-cell x-grid-col-' + col.mapping + ' ' + cls + '" mapping="' + col.mapping + '">' + col.header + '</td>';
		}
		colTpl += '    </tr>';
		colTpl += '</table>';
		return {
			dock      : "top",
			html      : colTpl
		};
	},
	
	/**
	 * 创建分页组件
	 * 
	 */
	buildPagenation:function()
	{
		var self = this,
			page = this.pagenation;
		var pagenationHtml = '<ul class="x-page-nl"/>';
		var pluginItems = '<div class="x-page-items"/>'
		return {
			dock : 'bottom',
			html : '<div class="x-grid-footer">'
				+		pagenationHtml
				+       pluginItems
				+  '</div>',
			listeners:{afterrender:function(t){
					if((el = t.getEl()))
					{
						var dh = Ext.DomHelper;
						var ul_queried = el.query(".x-page-nl");
						if((ul_queried && ul_queried.length > 0)){
							var page_ul = ul_queried[0];
							self.prv_page_btn = dh.append(page_ul,'<a class="x-page-control">' + com.hp.demo.vteam.dict.PAGE.PREV_PAGE + '</a>'),
								self.page_ipt = dh.insertAfter(self.prv_page_btn,'<input class="x-input-number" type="number" size="3"/>')
								self.turn_btn = dh.insertAfter(self.page_ipt, '<a class="x-page-control">' + com.hp.demo.vteam.dict.PAGE.TURN_TO + '</a>')
								self.next_page_btn = dh.insertAfter(self.turn_btn,'<a class="x-page-control">' + com.hp.demo.vteam.dict.PAGE.NEXT_PAGE + '</a>');

								self.prv_page_btn.onclick = Ext.createDelegate(self.to_prev,self);
								self.turn_btn.onclick = Ext.createDelegate(self.to_certain,self);
								self.next_page_btn.onclick = Ext.createDelegate(self.to_next,self);
						}
						
						var item_queried = el.query(".x-page-items");
						if(item_queried && item_queried.length > 0)
						{
							var items_div = item_queried[0];
							Ext.each(page,function(item){
								//创建各Id对应的item
								var item_btn = self[item + '_btn'] = dh.append(items_div,
									'<div class="x-page-item">' + com.hp.demo.vteam.dict.ITEM[item.id] + '</div>')
								item_btn.onclick = Ext.createDelegate(self[item.id],self);
							});
						}
					}
			}}
		};
	},
	
	buildDataView:function()
	{
		var self        = this,
			colModel  = self.colModel,
			colNum    = self.getColNum(),
			colTpl    = '<tr class="x-grid-row {isDirty:this.isRowDirty(parent)}">',
			cellWidth = 100/colNum;
		for (var i = 0; i < colModel.length; i++) {
			var col   = colModel[i],
				flex  = col.flex || 1,
				width = flex * cellWidth,
				style = (i === colModel.length - 1) ? "padding-right: 10px;" : "",
				cls   = col.cls || "";

			style += col.style || "";

			if (col.hidden) {
				cls += "x-grid-col-hidden";
			}
			colTpl += '<td width="' + width + '%" class="x-grid-cell x-grid-col-' + col.mapping + ' ' + cls + ' {isDirty:this.isCellDirty(parent)}" style="' + style + '" mapping="' + col.mapping + '" rowIndex="{rowIndex}">{' + col.mapping + '}</td>';
		}
		colTpl += '</tr>';
		return new Ext.DataView({
			store        : self.store,
			itemSelector : "tr.x-grid-row",
			simpleSelect : false,
			scroll       : self.scroll,
			flex         : 1,
			tpl          : new Ext.XTemplate(
				'<table style="width: 100%;">',
					'<tpl for=".">',
						colTpl,
					'</tpl>',
				'</table>',
				{
					isRowDirty: function(dirty, data) {
						return dirty ? "x-grid-row-dirty" : "";
					},
					isCellDirty: function(dirty, data) {
						return dirty ? "x-grid-cell-dirty" : "";
					}
				}
			),
			prepareData  : function(data, index, record) {
				var column,
					i  = 0,
					ln = colModel.length;

				data.dirtyFields = {};

				for (; i < ln; i++) {
					column = colModel[i];
					if (typeof column.renderer === "function") {
						data[column.mapping] = column.renderer.apply(self, [data[column.mapping]]);
					}
				}
				data.isDirty = record.dirty;

				data.rowIndex = index;
				return data;
			},
			listeners:{
				itemtap:{fn:self.onRowClick,scope:self},
				itemdoubletap:{fn:self.onRowDoubleClick,scope:self}
			}
		});
	},

	getColNum:function()
	{
		return this.colModel.length;
	},
	
	loadData:function(){
		var self = this;
		this.store.load({scope:this.store,callback:function(r,o,s){
			var rlt_set = o.resultSet;
			if(self.pagenation)
			{
				self.pages_count = parseInt(rlt_set.total/this.pageSize) + (rlt_set.total%this.pageSize!==0?1:0);
				self.page_ipt.min = 1;
				self.page_ipt.max = self.pages_count;
			}
		}});
	}
});
Ext.reg('grid',com.hp.demo.vteam.base.GridPanel);

com.hp.demo.vteam.base.AjaxSelector = Ext.extend(Ext.form.Select,{
	initComponent:function()
	{
		
		var selector = this,
			tempValue = selector.defalultValue||'';
		this.store.load({callback:function(records,o,s){
			com.hp.demo.vteam.base.AjaxSelector.superclass.initComponent.call(this);
			selector.setValue(tempValue);
			selector.fireEvent('change',selector,tempValue);
		}});
		
	},
	onRender:function(ct,options)
	{		
		com.hp.demo.vteam.base.AjaxSelector.superclass.onRender.call(this,ct,options);
	}
});
Ext.reg('ajaxselect',com.hp.demo.vteam.base.AjaxSelector);
//-------------------------通用方法以及类--------------------------------end 






//----------------------------------------------------------框架UI -----  begin
//-----private
//vteam主面板
com.hp.demo.vteam.base.MainFramePanel = Ext.extend(Ext.Panel,{
	initComponent : function(){
		var display_panel = new com.hp.demo.vteam.base.DisplayPanel();
		var nav_panel = new com.hp.demo.vteam.base.LogoAndNavPanel({displayHandler:display_panel});
		Ext.apply(this,{
			fullscreen: true,
			ui:'light',
			layout:{
				type: 'vbox',
				align:'left'
			},
			items:[
				nav_panel,
				display_panel
			]
		});
		com.hp.demo.vteam.base.MainFramePanel.superclass.initComponent.call(this);
	}
});

//vteam上方的商标以及总体导航菜单
com.hp.demo.vteam.base.LogoAndNavPanel = Ext.extend(Ext.Panel,{
	initComponent:function()
	{
		Ext.apply(this,{
			layout:'fit',
			/**
			 * 
			 */
			nav2component:function(componentId)
			{
				this.displayHandler.doNavAction(componentId);
			},
			html:'<div class=\"vteam_log\">'
				+ '<nav>'
				+ '<a id="' + com.hp.demo.vteam.dict.ID.INFO_PAGE + '">' + com.hp.demo.vteam.dict.NAV_TITLE.InfoPage + '</a>'
				+ '<a id="' + com.hp.demo.vteam.dict.ID.ACCOUNT_PAGE + '"> ' + com.hp.demo.vteam.dict.NAV_TITLE.AccountPage + '</a>'
				+ '<a id="helps">帮助</a>'
				+ '</nav>'
				+'</div>',
			listeners:{
				//完成渲染后,给vteam_log下的nav下的超链接添加点击事件以控制页面的加载
				'afterrender':{
					fn:function(){
						Ext.each(Ext.query('.vteam_log > nav > a'),function(item){
							navPanel = this;
							item.onclick = function(e){
								navPanel.nav2component(this.id);
								if(e && e.stopPropagation)
								{
									e.stopPropagation();
								}
								return false;
							};
						},this);
					},
					scope:this
				}
			}
		});
		com.hp.demo.vteam.base.LogoAndNavPanel.superclass.initComponent.call(this);
	},
	
});

/*
 * 内容展示容器
 * 用于加载各子component,并提供基本的加载动画,提交的进度动画等回调函数
 *
 */
com.hp.demo.vteam.base.DisplayPanel = Ext.extend(Ext.Panel,{
	initComponent:function()
	{
		Ext.apply(this,{
			flex:1,
			layout:'vbox',
			//调用本函数加载显示内容
			doNavAction:function(componentId)
			{
				com.hp.demo.vteam.base.Action(
					componentId,
					com.hp.demo.vteam.dict.NAV_TITLE[componentId]
				);
			},
			dockedItems:[
				{
					xtype:'panel',
					baseCls: 'x-toolbar',
					ui:'light',
					dock:'bottom',
					html:'<div id="status_msg" style="height:25px;line-height:25px;"></div>'
				}
			],
			listeners:{
				'afterrender':{
					fn:function(){
						var change_status_txt = function(msg)
						{
							Ext.query('#status_msg')[0].innerHTML = msg;
						};
						//给displayPanel 绑定加载的状态回调函数
						this.doLoad = function(msg)
						{
							if(change_status_txt.prototype.timeoutid)
							{
								clearTimeout(change_status_txt.prototype.timeoutid);
							}
							change_status_txt(msg);
							change_status_txt.prototype.timeoutid = setTimeout(function(){change_status_txt('')},3000);
						};
						com.hp.demo.vteam.base.ComponentLoader = Ext.createDelegate(com.hp.demo.vteam.base.ComponentLoader,this);
						com.hp.demo.vteam.base.status_msg = Ext.createDelegate(com.hp.demo.vteam.base.status_msg,this);
					},
					scope:this
				}
			}
		});
		com.hp.demo.vteam.base.DisplayPanel.superclass.initComponent.call(this);
	}
});
//----------------------------------------------------------框架UI -----  end